﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Araman.Models.UserModels
{
    using Araman.Models;
    using System.Web.Mvc;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using Araman.Resources;
    public class UserGroup
    {
        [Key()]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int GroupId { get; set; }


        [MaxLength(64, ErrorMessageResourceName = "UserGroupName_MaxLength", ErrorMessageResourceType = typeof(LngModelErrors))]
        [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "UserGroupName_Null", ErrorMessageResourceType = typeof(LngModelErrors))]
        [Display(ResourceType = typeof(LngModelDisplayNames), Name = "UserGroupName")]
        public string GroupName { get; set; }

        [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "UserGroupName_LanguageKey_Null", ErrorMessageResourceType = typeof(LngModelErrors))]
        [MaxLength(64, ErrorMessageResourceName = "UserGroupName_LanguageKey_MaxLength", ErrorMessageResourceType = typeof(LngModelErrors))]
        [Display(ResourceType = typeof(LngModelDisplayNames), Name = "UserGroupName_LanguageKey")]
        public string GroupNameLanguageKey { get; set; }

        public virtual IEnumerable<UserPermission> Permissions { get; set; }
        public virtual IEnumerable<Userr> Users { get; set; }

        public static UserGroup GetUserGroupFromId(int groupId)
        {
            if (groupId <= 0)
                return null;

            return (from gr in DbModel.Current.UserGroups where gr.GroupId == groupId select gr).FirstOrDefault<UserGroup>();
        }
        public static UserGroup GetUserGroupFromName(string groupName)
        {
            if (String.IsNullOrEmpty(groupName.Trim()))
                return null;

            return (from gr in DbModel.Current.UserGroups where gr.GroupName == groupName select gr).FirstOrDefault<UserGroup>();
        }
        public static UserGroup Group_Administrators { get { return GetUserGroupFromName("Administrators"); } }
        public static UserGroup Group_StandartUsers { get { return GetUserGroupFromName("StandartUsers"); } }

    }
}